<template>
    <div class="card" ref="cardRef" @mousemove="handleMouseMove" @mouseleave="handleMouseLeave">
        <div class="card-item" v-for="v in list">
            <div class="card-item-inner">
                <span>{{ v }}</span>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue';
let list = ref([
    'CSS的效果1',
    'CSS的效果2',
    'CSS的效果3',
    'CSS的效果4',
    'CSS的效果5',
    'CSS的效果6',
    'CSS的效果7',
    'CSS的效果8',
    'CSS的效果9',
])

let cardRef = ref();
const handleMouseMove = (e) =>{
    let cardItems = cardRef.value.children;
    for(const item of cardItems){
        const rect = item.getBoundingClientRect();
        let x = e.clientX - rect.left;
        let y = e.clientY - rect.top;
        item.style.setProperty('--moveX',`${x}px`);
        item.style.setProperty('--moveY',`${y}px`);
    }
}
const handleMouseLeave = () =>{
    let cardItems = cardRef.value.children;
    for(const item of cardItems){
        item.style.setProperty('--moveX',`1000px`);
        item.style.setProperty('--moveY',`1000px`);
    }
}
</script>

<style scoped>
.card {
    width: 600px;
    height: 600px;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-gap: 10px;
}

.card-item {
    box-shadow: 0px 0px 4px #cccc;
    position: relative;
    border-radius: 12px;
    overflow: hidden;
}

.card-item-inner {
    inset: 2px;
    background-color: white;
    position: absolute;
    display: flex;
    border-radius: inherit;
    z-index: 3;
}

.card-item::after {
    content: '';
    position: absolute;
    z-index: 2;
    width: 100%;
    height: 100%;
    left: var(--moveX, 1000px);
    top: var(--moveY, 1000px);
    border-radius: inherit;
    transform: translate(-50%, -50%);
    background: radial-gradient(closest-side circle,
            red 0%,
            gold 50%,
            transparent);
}

.card-item-inner span {
    margin: auto;
    font-size: 14px;
    font-weight: 600;
}
</style>